$(function(){
    var listUrl = '/shop/listProductsByShop?page=0&size=100';

    var deleteUrl = '/shop/deleteProduct';
    getList();
    function getList(){
        $.getJSON(listUrl,function(data){
            if(data.success){
                var productList = data.productList;
                var tempHtml = '';
                productList.map(function(item,index){
                    var textOp = '下架';
                    var contraryStatus = 0;
                    if(item.enableStatus == 0){
                        textOp = '上架';
                        contraryStatus = 1;
                    }else{
                        contraryStatus = 0;
                    }
                    tempHtml += ''+'<div class="row row-product">'
                    + '<div class="col-40"> '
                    + item.productName +'</div>'
                    +'<div class="col-10">'
                    + item.priority
                    +'</div>'
                    +'<div class="col-50">'
                    +'<a href="#" class="edit" data-id="'
                    +item.productId
                    +'"data-status="'
                    +item.enableStatus
                    +'">编辑</a>'
                    +'<a href="#" class="delete" data-id="'
                    +item.productId
                    +'"data-status="'
                    +contraryStatus
                    +'">'
                    +textOp
                    +'</a>'
                    +'<a href="#" class="preview" data-id=" '
                    + item.productId
                    +'" data-status="'
                    +item.enableStatus
                    +'">预览</a>'
                    +'</div>'
                    +'</div>';
                });
                $('.product-wrap').html(tempHtml);
            }
        });
    }

    $('.product-wrap').on('click','a',function(e){
        var target = $(e.currentTarget);
        if(target.hasClass('edit')){
            window.location.href="/shop/productedit?productId="
            + e.currentTarget.dataset.id;
        }else if(target.hasClass('delete')){
            deleteItem(e.currentTarget.dataset.id,e.currentTarget.dataset.status);
        }else if(target.hasClass('preview')){
            window.location.href="/dashboard/productdetail?productId="
            +e.currentTarget.dataset.id;
        }
    });

    function deleteItem(id,enableStatus){

        var productId = id;
        var enableStatus = enableStatus;
        $.confirm('确定吗？',function(){
            $.ajax({
                url:deleteUrl+'?productId='+productId+'&enableStatus='+enableStatus,
                type:'GET',
                data:{},
                dataType:'json',
                success:function(data){
                    if(data.success){
                        $.toast('操作成功');
                        getList();
                    }else{
                        $.toast('操作失败');
                    }
                }
            });
        });

    }

    $('#new').click(function(){
        window.location.href='/shop/productedit';
    });
})


